home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gdevmswn.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  22KB  |  746 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevmswn.c */
  20. /*
  21.  * Microsoft Windows 3.n driver for Ghostscript.
  22.  * Original version by Russell Lang and Maurice Castro with help from
  23.  * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  24.  * created from gdevbgi.c and gnuplot/term/win.trm 5th June 1992.
  25.  * Extensively modified by L. Peter Deutsch, Aladdin Enterprises.
  26.  */
  27. #include "gdevmswn.h"
  28. #include "gp.h"
  29. #include "gpcheck.h"
  30. #include "gsprops.h"
  31. #include "gdevpccm.h"
  32.  
  33. /* Forward references */
  34. private void near win_makeimg(P1(gx_device_win *));
  35. LRESULT CALLBACK _export WndImgProc(HWND, UINT, WPARAM, LPARAM);
  36. LRESULT CALLBACK _export WndImgChildProc(HWND, UINT, WPARAM, LPARAM);
  37.  
  38. /* Define the repaint interval in milliseconds. */
  39. /* Suggested values: 10000 for 286, 5000 for 386, 3000 for 486. */
  40. #define repaint_interval 5000
  41. #define TIMER_ID 1
  42.  
  43. /* Open the win driver */
  44. int
  45. win_open(gx_device *dev)
  46. {    HDC hdc;
  47.     int depth;
  48.     WNDCLASS wndclass;
  49.     static BOOL registered = FALSE;
  50.  
  51.     /* Initialize the scrolling information. */
  52.     
  53.     wdev->cxClient = wdev->cyClient = 0;
  54.     wdev->nVscrollPos = wdev->nVscrollMax = 0;
  55.     wdev->nHscrollPos = wdev->nHscrollMax = 0;
  56.  
  57.     wdev->update = wdev->timer = FALSE;
  58.  
  59.     if (dev->width == INITIAL_WIDTH)
  60.       dev->width  = (int)(8.5  * dev->x_pixels_per_inch);
  61.     if (dev->height == INITIAL_HEIGHT)
  62.       dev->height = (int)(11.0 * dev->y_pixels_per_inch);
  63.  
  64.     /* If this is the first instance, register the window classes. */
  65.     if (!registered) {
  66.         /* register the window class for graphics */
  67.         wndclass.style = CS_HREDRAW | CS_VREDRAW;
  68.         wndclass.lpfnWndProc = WndImgChildProc;
  69.         wndclass.cbClsExtra = 0;
  70.         wndclass.cbWndExtra = sizeof(LONG);
  71.         wndclass.hInstance = phInstance;
  72.         wndclass.hIcon = LoadIcon(phInstance,"grpicon");
  73.         wndclass.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
  74.         wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  75.         wndclass.lpszMenuName = NULL;
  76.         wndclass.lpszClassName = szAppName;
  77.         RegisterClass(&wndclass);
  78.  
  79.         wndclass.style = CS_HREDRAW | CS_VREDRAW;
  80.         wndclass.lpfnWndProc = WndImgProc;
  81.         wndclass.cbClsExtra = 0;
  82.         wndclass.cbWndExtra = sizeof(LONG);
  83.         wndclass.hInstance = phInstance;
  84.         wndclass.hIcon = LoadIcon(phInstance,"grpicon");
  85.         wndclass.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
  86.         wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  87.         wndclass.lpszMenuName = NULL;
  88.         wndclass.lpszClassName = szImgName;
  89.         RegisterClass(&wndclass);
  90.     
  91.         registered = TRUE;
  92.     }
  93.  
  94.     win_makeimg(wdev);
  95.     wdev->hdctext = NULL;
  96.  
  97.     /* Set parameters that were unknown before opening device */
  98.     /* Find out if the device supports color */
  99.     /* We recognize 2, 16 or 256 color devices */
  100.     hdc = GetDC(wdev->hwndimgchild);
  101.     depth = GetDeviceCaps(hdc,PLANES) * GetDeviceCaps(hdc,BITSPIXEL);
  102.     ReleaseDC(wdev->hwndimgchild,hdc);
  103.     if ( depth >= 8 ) { /* use 64 static colors and 166 dynamic colors from 8 planes */
  104.         static const gx_device_color_info win_256color = dci_color(8,31,4);
  105.         dev->color_info = win_256color;
  106.         wdev->nColors = 64;
  107.     }
  108.     else if ( depth >= 4 ) {
  109.         static const gx_device_color_info win_16ega_color = dci_ega;
  110.         static const gx_device_color_info win_16vga_color = dci_vga;
  111.         hdc = GetDC(NULL);
  112.         if (GetDeviceCaps(hdc, VERTRES) <= 350)
  113.             dev->color_info = win_16ega_color;
  114.         else
  115.             dev->color_info = win_16vga_color;
  116.         ReleaseDC(NULL,hdc);
  117.         wdev->nColors = 16;
  118.     } 
  119.     else {   /* default is black_and_white */
  120.         wdev->nColors = 2;
  121.     }
  122.  
  123.     /* create palette for display */
  124.     if ((wdev->limgpalette = win_makepalette(wdev))
  125.         == (LPLOGPALETTE)NULL)
  126.         return win_nomemory();
  127.     wdev->himgpalette = CreatePalette(wdev->limgpalette);
  128.  
  129.     return 0;
  130. }
  131.  
  132. /* Make the output appear on the screen. */
  133. int
  134. win_sync_output(gx_device *dev)
  135. {
  136.     if (wdev->timer)
  137.         KillTimer(wdev->hwndimgchild, TIMER_ID);
  138.     wdev->timer = FALSE;
  139.     wdev->update = FALSE;
  140.     if (gsview) {
  141.     SendMessage(gsview_hwnd, WM_GSVIEW, SYNC_OUTPUT, (LPARAM)NULL);
  142.     }
  143.     else {
  144.     if ( !IsWindow(wdev->hwndimg) ) {  /* some clod closed the window */
  145.         win_makeimg(wdev);
  146.     }
  147.     if ( !IsIconic(wdev->hwndimg) ) {  /* redraw window */
  148.         InvalidateRect(wdev->hwndimg, NULL, 1);
  149.         UpdateWindow(wdev->hwndimg);
  150.     }
  151.     }
  152.     return(0);
  153. }
  154.  
  155. /* Make the window visible, and display the output. */
  156. int
  157. win_output_page(gx_device *dev, int copies, int flush)
  158. {
  159.     if (wdev->timer)
  160.         KillTimer(wdev->hwndimgchild, TIMER_ID);
  161.     wdev->timer = FALSE;
  162.     wdev->update = FALSE;
  163.  
  164.     if (gsview) {
  165.         SendMessage(gsview_hwnd, WM_GSVIEW, OUTPUT_PAGE, (LPARAM)NULL);
  166.         gsview_next = FALSE;
  167.         /* wait for NEXT_PAGE message */
  168.         while (!gsview_next)
  169.         gp_check_interrupts();
  170.         return(0);
  171.     }
  172.     else {
  173.         if (IsIconic(wdev->hwndimg))    /* useless as an Icon so fix it */
  174.         ShowWindow(wdev->hwndimg, SW_SHOWNORMAL);
  175.         BringWindowToTop(wdev->hwndimg);
  176.     }
  177.     return( win_sync_output(dev) );
  178. }
  179.  
  180. /* Close the win driver */
  181. int
  182. win_close(gx_device *dev)
  183. {
  184.     /* Free resources */
  185.     if (wdev->timer)
  186.         KillTimer(wdev->hwndimgchild, TIMER_ID);
  187.     wdev->timer = FALSE;
  188.     wdev->update = FALSE;
  189.  
  190.     DeleteObject(wdev->himgpalette);
  191.     gs_free((char *)(wdev->limgpalette), 1, sizeof(LOGPALETTE) + 
  192.         (1<<(wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  193.         "win_close");
  194.     if (gsview)
  195.         DestroyWindow(wdev->hwndimgchild);
  196.     else
  197.         DestroyWindow(wdev->hwndimg);
  198.     gp_check_interrupts();    /* process WIN_DESTROY message */
  199.  
  200.     return(0);
  201. }
  202.  
  203. /* Map a r-g-b color to the colors available under Windows */
  204. gx_color_index
  205. win_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  206.   gx_color_value b)
  207. {
  208.     switch(dev->color_info.depth) {
  209.       case 8: {
  210.         int i;
  211.         LPLOGPALETTE lpal = wdev->limgpalette;
  212.         PALETTEENTRY *pep;
  213.         byte cr, cg, cb;
  214.  
  215.         /* map colors to 0->255 in 32 steps */
  216.         cr = win_color_value(r);
  217.         cg = win_color_value(g);
  218.         cb = win_color_value(b);
  219.  
  220.         /* search in palette */
  221.         for ( i = 0, pep = &lpal->palPalEntry[i];
  222.               i < wdev->nColors; i++, pep++
  223.             )
  224.         {    if ( !((cr ^ pep->peRed) & 0xf8) &&
  225.                  !((cg ^ pep->peGreen) & 0xf8) &&
  226.                  !((cb ^ pep->peBlue) & 0xf8)
  227.                )
  228.                 return((gx_color_index)i);    /* found it */
  229.         }
  230.         
  231.         /* next try adding it to palette */
  232.         if (i < 220) { /* allow 36 for windows and other apps */
  233.             LPLOGPALETTE lipal = wdev->limgpalette;
  234.             wdev->nColors = i+1;
  235.  
  236.             DeleteObject(wdev->himgpalette);
  237.              lipal->palPalEntry[i].peFlags = 0;
  238.             lipal->palPalEntry[i].peRed   =  cr;
  239.             lipal->palPalEntry[i].peGreen =  cg;
  240.             lipal->palPalEntry[i].peBlue  =  cb;
  241.             lipal->palNumEntries = wdev->nColors;
  242.             wdev->himgpalette = CreatePalette(lipal);
  243.  
  244.             return((gx_color_index)i);    /* return new palette index */
  245.         }
  246.  
  247.         return(gx_no_color_index);  /* not found - dither instead */
  248.         }
  249.       case 4:
  250.         if ((r == g) && (g == b) && (r >= gx_max_color_value / 3 * 2 - 1)
  251.            && (r < gx_max_color_value / 4 * 3))
  252.             return ((gx_color_index)8);    /* light gray */
  253.         return pc_4bit_map_rgb_color(dev, r, g, b);
  254.     }
  255.     return (gx_default_map_rgb_color(dev,r,g,b));
  256. }
  257.  
  258. /* Map a color code to r-g-b. */
  259. int
  260. win_map_color_rgb(gx_device *dev, gx_color_index color,
  261.   gx_color_value prgb[3])
  262. {
  263. gx_color_value one;
  264.     switch(dev->color_info.depth) {
  265.       case 8:
  266.         one = (gx_color_value) (gx_max_color_value / 255);
  267.         prgb[0] = wdev->limgpalette->palPalEntry[(int)color].peRed * one;
  268.         prgb[1] = wdev->limgpalette->palPalEntry[(int)color].peGreen * one;
  269.         prgb[2] = wdev->limgpalette->palPalEntry[(int)color].peBlue * one;
  270.         break;
  271.       case 4:
  272.         if (color == 8)    /* VGA light gray */
  273.             prgb[0] = prgb[1] = prgb[2] = (gx_max_color_value / 4 * 3);
  274.         else
  275.             pc_4bit_map_color_rgb(dev, color, prgb);
  276.         break;
  277.       default:
  278.         prgb[0] = prgb[1] = prgb[2] = 
  279.             (int)color ? gx_max_color_value : 0;
  280.     }
  281.     return 0;
  282. }
  283.  
  284. /* Standard properties for windows. */
  285. private const gs_prop_item win_props_std[] = {
  286.     prop_def("HWResolution", prt_float_array),
  287.     prop_def("HWSize", prt_int_array),
  288.         /* Slots for arrays */
  289.     prop_float, prop_float,
  290.     prop_int, prop_int
  291. };
  292.  
  293. /* Set window properties -- size and resolution. */
  294. /* We implement this ourselves so that we can do it without */
  295. /* closing and opening the device. */
  296. int
  297. win_put_props(gx_device *dev, gs_prop_item *plist, int count)
  298. {    gs_prop_item *known[2];
  299.     int code = 0;
  300.     gx_device temp_dev;
  301.     props_extract(plist, count, win_props_std, 2, known, 1);
  302.     temp_dev = *dev;
  303.     if ( known[1] != 0 )
  304.        {    if ( known[1]->value.a.size != 2 )
  305.             known[1]->status = pv_typecheck,
  306.             code = gs_error_typecheck;
  307.         else
  308.            {    gs_prop_item *ap = known[1]->value.a.p.v;
  309.             if ( ap[0].value.i <= 0 || ap[0].value.i > 0x7fff ||
  310.                  ap[1].value.i <= 0 || ap[1].value.i > 0x7fff
  311.                )
  312.                 known[1]->status = pv_rangecheck,
  313.                 code = gs_error_rangecheck;
  314.             else
  315.                {    temp_dev.width = ap[0].value.i;
  316.                 temp_dev.height = ap[1].value.i;
  317.                }
  318.             if ( code == 0 ) code = 1;
  319.            }
  320.        }
  321.     if ( known[0] != 0 )
  322.        {    if ( known[0]->value.a.size != 2 )
  323.             known[0]->status = pv_typecheck,
  324.             code = gs_error_typecheck;
  325.         else
  326.            {    gs_prop_item *ap = known[0]->value.a.p.v;
  327.             if ( ap[0].value.f <= 0 || ap[1].value.f <= 0 )
  328.                 known[0]->status = pv_rangecheck,
  329.                 code = gs_error_rangecheck;
  330.             else
  331.                {    temp_dev.x_pixels_per_inch = ap[0].value.f;
  332.                 temp_dev.y_pixels_per_inch = ap[1].value.f;
  333.                }
  334.             if ( code == 0 ) code = 1;
  335.            }
  336.        }
  337.     if ( code < 0 )
  338.         return_error(code);
  339.     /* Hand off the change to the implementation. */
  340.     if ( dev->is_open && code )
  341.     {    int ccode;
  342.         (*wdev->free_bitmap)(wdev);
  343.         ccode = (*wdev->alloc_bitmap)(wdev, &temp_dev);
  344.         if ( ccode < 0 )
  345.         {    (*wdev->alloc_bitmap)(wdev, dev);
  346.             return ccode;
  347.         }
  348.     }
  349.     dev->x_pixels_per_inch = temp_dev.x_pixels_per_inch;
  350.     dev->y_pixels_per_inch = temp_dev.y_pixels_per_inch;
  351.     dev->width = temp_dev.width;
  352.     dev->height = temp_dev.height;
  353.     if (IsWindow(wdev->hwndimg) && IsWindow(wdev->hwndimgchild)) {
  354.         RECT rect;
  355.         /* erase bitmap - before window gets redrawn */
  356.         (*dev->procs->fill_rectangle)(dev, 0, 0, dev->width, dev->height,
  357.         win_map_rgb_color(dev, gx_max_color_value, 
  358.             gx_max_color_value, gx_max_color_value));
  359.         /* cause scroll bars to be redrawn */
  360.         GetClientRect(wdev->hwndimgchild,&rect);
  361.         SendMessage(wdev->hwndimgchild, WM_SIZE, SIZE_RESTORED, 
  362.         MAKELPARAM(rect.right-rect.left, rect.bottom-rect.top));
  363.     }
  364.     return 0;
  365. }
  366.  
  367.  
  368. /* ------ Internal routines ------ */
  369.  
  370. #undef wdev
  371.  
  372. /* make image window */
  373. private void near 
  374. win_makeimg(gx_device_win *wdev)
  375. {    HMENU sysmenu;
  376.     RECT rect;
  377.     /* parent window */
  378.     if (gsview) {
  379.         wdev->hwndimg = gsview_hwnd;
  380.         if (IsIconic(wdev->hwndimg))
  381.             ShowWindow(wdev->hwndimg, SW_SHOWNORMAL);
  382.     }
  383.     else {
  384.         wdev->hwndimg = CreateWindow(szImgName, (LPSTR)szImgName,
  385.           WS_OVERLAPPEDWINDOW,
  386.           CW_USEDEFAULT, CW_USEDEFAULT, 
  387.           CW_USEDEFAULT, CW_USEDEFAULT, 
  388.           NULL, NULL, phInstance, (void FAR *)wdev);
  389.         ShowWindow(wdev->hwndimg, SW_SHOWMINNOACTIVE);
  390.     }
  391.     /* child for graphics */
  392.     GetClientRect(wdev->hwndimg, &rect);
  393.     CreateWindow(szAppName, (LPSTR)szImgName,
  394.           WS_CHILD | WS_VSCROLL | WS_HSCROLL,
  395.           rect.left, rect.top,
  396.           rect.right-rect.left, rect.bottom-rect.top, 
  397.           wdev->hwndimg, NULL, phInstance, (void FAR *)wdev);
  398.     ShowWindow(wdev->hwndimgchild, SW_SHOWNORMAL);
  399.  
  400.     if (!gsview) {
  401.         /* modify the menu to have the new items we want */
  402.         sysmenu = GetSystemMenu(wdev->hwndimg,0);    /* get the sysmenu */
  403.         AppendMenu(sysmenu, MF_SEPARATOR, 0, NULL);
  404.         AppendMenu(sysmenu, MF_STRING, M_COPY_CLIP, "Copy to Clip&board");
  405.     }
  406. }
  407.  
  408.  
  409. /* out of memory error message box */
  410. int
  411. win_nomemory(void)
  412. {
  413.            MessageBox(hwndtext,(LPSTR)"Not enough memory",(LPSTR) szAppName, MB_ICONSTOP);
  414.     return gs_error_limitcheck;
  415. }
  416.  
  417.  
  418. LPLOGPALETTE
  419. win_makepalette(gx_device_win *wdev)
  420. {    int i, val;
  421.     LPLOGPALETTE logpalette;
  422.     logpalette = (LPLOGPALETTE)gs_malloc(1, sizeof(LOGPALETTE) + 
  423.         (1<<(wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  424.         "win_makepalette");
  425.     if (logpalette == (LPLOGPALETTE)NULL)
  426.         return(0);
  427.     logpalette->palVersion = 0x300;
  428.     logpalette->palNumEntries = wdev->nColors;
  429.     for (i=0; i<wdev->nColors; i++) {
  430.       logpalette->palPalEntry[i].peFlags = 0;
  431.       switch (wdev->nColors) {
  432.         case 64:
  433.           /* colors are rrggbb */
  434.           logpalette->palPalEntry[i].peRed   = ((i & 0x30)>>4)*85;
  435.           logpalette->palPalEntry[i].peGreen = ((i & 0xC)>>2)*85;
  436.           logpalette->palPalEntry[i].peBlue  = (i & 3)*85;
  437.           break;
  438.         case 16:
  439.           /* colors are irgb */
  440.           val = (i & 8 ? 255 : 128);
  441.           logpalette->palPalEntry[i].peRed   = i & 4 ? val : 0;
  442.           logpalette->palPalEntry[i].peGreen = i & 2 ? val : 0;
  443.           logpalette->palPalEntry[i].peBlue  = i & 1 ? val : 0;
  444.           if (i == 8) { /* light gray */
  445.               logpalette->palPalEntry[i].peRed   = 
  446.               logpalette->palPalEntry[i].peGreen = 
  447.               logpalette->palPalEntry[i].peBlue  = 192;
  448.           }
  449.           break;
  450.         case 2:
  451.           logpalette->palPalEntry[i].peRed =
  452.             logpalette->palPalEntry[i].peGreen =
  453.             logpalette->palPalEntry[i].peBlue = (i ? 255 : 0);
  454.           break;
  455.       }
  456.     }
  457.     return(logpalette);
  458. }
  459.  
  460.  
  461. void
  462. win_update(gx_device_win *wdev)
  463. {
  464.     wdev->update = TRUE;
  465.     if (!wdev->timer) {
  466.         SetTimer(wdev->hwndimgchild, TIMER_ID, repaint_interval, NULL);
  467.         wdev->timer = TRUE;
  468.       }
  469. }
  470.  
  471.  
  472. /* child graphics window */
  473. LRESULT CALLBACK _export
  474. WndImgChildProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  475. {    gx_device_win *wdev;
  476.     HDC hdc;
  477.     PAINTSTRUCT ps;
  478.     RECT rect;
  479.     HPALETTE oldpalette;
  480.     int nVscrollInc, nHscrollInc;
  481.  
  482.     wdev = (gx_device_win *)GetWindowLong(hwnd, 0);
  483.  
  484.     switch(message) {
  485.         case WM_CREATE:
  486.             wdev =  (gx_device_win *)(((CREATESTRUCT *)lParam)->lpCreateParams);
  487.             SetWindowLong(hwnd, 0, (LONG)wdev);
  488.             wdev->hwndimgchild = hwnd;
  489.             if (gsview)
  490.                 SendMessage(gsview_hwnd, WM_GSVIEW, HWND_IMGCHILD, (LPARAM)wdev->hwndimgchild);
  491. #if WINVER >= 0x030a
  492.             /* Enable Drag Drop */
  493.             if (is_win31)
  494.                 DragAcceptFiles(hwnd, TRUE);
  495. #endif
  496.             break;
  497.         case WM_GSVIEW:
  498.             switch(wParam) {
  499.                 case NEXT_PAGE:
  500.                     gsview_next = TRUE;
  501.                     return 0;
  502.                 case COPY_CLIPBOARD:
  503.                     (*wdev->copy_to_clipboard)(wdev);
  504.                     return 0;
  505.             }
  506.             break;
  507.         case WM_TIMER:
  508.             if ((wParam == TIMER_ID) && wdev->update) {
  509.                 InvalidateRect(wdev->hwndimgchild, (LPRECT)NULL, FALSE);
  510.                 UpdateWindow(wdev->hwndimgchild);
  511.                 wdev->update = FALSE;
  512.             }
  513.             break;
  514.         case WM_SIZE:
  515.             if (wParam == SIZE_MINIMIZED)
  516.                 return(0);
  517.             wdev->cyClient = HIWORD(lParam);
  518.             wdev->cxClient = LOWORD(lParam);
  519.  
  520.             wdev->cyAdjust = min(wdev->height, wdev->cyClient) - wdev->cyClient;
  521.             wdev->cyClient += wdev->cyAdjust;
  522.  
  523.             wdev->nVscrollMax = max(0, wdev->height - wdev->cyClient);
  524.             wdev->nVscrollPos = min(wdev->nVscrollPos, wdev->nVscrollMax);
  525.  
  526.             SetScrollRange(hwnd, SB_VERT, 0, wdev->nVscrollMax, FALSE);
  527.             SetScrollPos(hwnd, SB_VERT, wdev->nVscrollPos, TRUE);
  528.  
  529.             wdev->cxAdjust = min(wdev->width,  wdev->cxClient) - wdev->cxClient;
  530.             wdev->cxClient += wdev->cxAdjust;
  531.  
  532.             wdev->nHscrollMax = max(0, wdev->width - wdev->cxClient);
  533.             wdev->nHscrollPos = min(wdev->nHscrollPos, wdev->nHscrollMax);
  534.  
  535.             SetScrollRange(hwnd, SB_HORZ, 0, wdev->nHscrollMax, FALSE);
  536.             SetScrollPos(hwnd, SB_HORZ, wdev->nHscrollPos, TRUE);
  537.  
  538.             if (gsview && wdev->cxAdjust > 0)    /* don't make gsview window grow */
  539.                 wdev->cxAdjust = 0;
  540.             if (gsview && wdev->cyAdjust > 0)
  541.                 wdev->cyAdjust = 0;
  542.  
  543.             if ((wParam==SIZENORMAL) && (wdev->cxAdjust!=0 || wdev->cyAdjust!=0)) {
  544.                 GetWindowRect(GetParent(hwnd),&rect);
  545.                 MoveWindow(GetParent(hwnd),rect.left,rect.top,
  546.                 rect.right-rect.left+wdev->cxAdjust,
  547.                 rect.bottom-rect.top+wdev->cyAdjust, TRUE);
  548.                 wdev->cxAdjust = wdev->cyAdjust = 0;
  549.             }
  550.             if (gsview)
  551.                 SendMessage(gsview_hwnd, WM_GSVIEW, SCROLL_POSITION, 
  552.                     MAKELPARAM(wdev->nHscrollPos, wdev->nVscrollPos));
  553.             return(0);
  554.         case WM_VSCROLL:
  555.             switch(LOWORD(wParam)) {
  556.                 case SB_TOP:
  557.                     nVscrollInc = -wdev->nVscrollPos;
  558.                     break;
  559.                 case SB_BOTTOM:
  560.                     nVscrollInc = wdev->nVscrollMax - wdev->nVscrollPos;
  561.                     break;
  562.                 case SB_LINEUP:
  563.                     nVscrollInc = -wdev->cyClient/16;
  564.                     break;
  565.                 case SB_LINEDOWN:
  566.                     nVscrollInc = wdev->cyClient/16;
  567.                     break;
  568.                 case SB_PAGEUP:
  569.                     nVscrollInc = min(-1,-wdev->cyClient);
  570.                     break;
  571.                 case SB_PAGEDOWN:
  572.                     nVscrollInc = max(1,wdev->cyClient);
  573.                     break;
  574.                 case SB_THUMBPOSITION:
  575.                     nVscrollInc = LOWORD(lParam) - wdev->nVscrollPos;
  576.                     break;
  577.                 default:
  578.                     nVscrollInc = 0;
  579.                 }
  580.             if ((nVscrollInc = max(-wdev->nVscrollPos, 
  581.                 min(nVscrollInc, wdev->nVscrollMax - wdev->nVscrollPos)))!=0) {
  582.                 wdev->nVscrollPos += nVscrollInc;
  583.                 ScrollWindow(hwnd,0,-nVscrollInc,NULL,NULL);
  584.                 SetScrollPos(hwnd,SB_VERT,wdev->nVscrollPos,TRUE);
  585.                 UpdateWindow(hwnd);
  586.             }
  587.             if (gsview)
  588.                 SendMessage(gsview_hwnd, WM_GSVIEW, SCROLL_POSITION, 
  589.                     MAKELPARAM(wdev->nHscrollPos, wdev->nVscrollPos));
  590.             return(0);
  591.         case WM_HSCROLL:
  592.             switch(LOWORD(wParam)) {
  593.                 case SB_LINEUP:
  594.                     nHscrollInc = -wdev->cxClient/16;
  595.                     break;
  596.                 case SB_LINEDOWN:
  597.                     nHscrollInc = wdev->cyClient/16;
  598.                     break;
  599.                 case SB_PAGEUP:
  600.                     nHscrollInc = min(-1,-wdev->cxClient);
  601.                     break;
  602.                 case SB_PAGEDOWN:
  603.                     nHscrollInc = max(1,wdev->cxClient);
  604.                     break;
  605.                 case SB_THUMBPOSITION:
  606.                     nHscrollInc = LOWORD(lParam) - wdev->nHscrollPos;
  607.                     break;
  608.                 default:
  609.                     nHscrollInc = 0;
  610.                 }
  611.             if ((nHscrollInc = max(-wdev->nHscrollPos, 
  612.                 min(nHscrollInc, wdev->nHscrollMax - wdev->nHscrollPos)))!=0) {
  613.                 wdev->nHscrollPos += nHscrollInc;
  614.                 ScrollWindow(hwnd,-nHscrollInc,0,NULL,NULL);
  615.                 SetScrollPos(hwnd,SB_HORZ,wdev->nHscrollPos,TRUE);
  616.                 UpdateWindow(hwnd);
  617.             }
  618.             if (gsview)
  619.                 SendMessage(gsview_hwnd, WM_GSVIEW, SCROLL_POSITION, 
  620.                     MAKELPARAM(wdev->nHscrollPos, wdev->nVscrollPos));
  621.             return(0);
  622.         case WM_KEYDOWN:
  623.             switch(LOWORD(wParam)) {
  624.                 case VK_HOME:
  625.                     SendMessage(hwnd,WM_VSCROLL,SB_TOP,0L);
  626.                     break;
  627.                 case VK_END:
  628.                     SendMessage(hwnd,WM_VSCROLL,SB_BOTTOM,0L);
  629.                     break;
  630.                 case VK_PRIOR:
  631.                     SendMessage(hwnd,WM_VSCROLL,SB_PAGEUP,0L);
  632.                     break;
  633.                 case VK_NEXT:
  634.                     SendMessage(hwnd,WM_VSCROLL,SB_PAGEDOWN,0L);
  635.                     break;
  636.                 case VK_UP:
  637.                     SendMessage(hwnd,WM_VSCROLL,SB_LINEUP,0L);
  638.                     break;
  639.                 case VK_DOWN:
  640.                     SendMessage(hwnd,WM_VSCROLL,SB_LINEDOWN,0L);
  641.                     break;
  642.                 case VK_LEFT:
  643.                     SendMessage(hwnd,WM_HSCROLL,SB_PAGEUP,0L);
  644.                     break;
  645.                 case VK_RIGHT:
  646.                     SendMessage(hwnd,WM_HSCROLL,SB_PAGEDOWN,0L);
  647.                     break;
  648.             }
  649.             return(0);
  650.         case WM_PAINT:
  651.             {
  652.             int sx,sy,wx,wy,dx,dy;
  653.             hdc = BeginPaint(hwnd, &ps);
  654.             oldpalette = SelectPalette(hdc,wdev->himgpalette,NULL);
  655.             RealizePalette(hdc);
  656.             SetMapMode(hdc, MM_TEXT);
  657.             SetBkMode(hdc,OPAQUE);
  658.             GetClientRect(hwnd, &rect);
  659.             SetViewportExt(hdc, rect.right, rect.bottom);
  660.             dx = rect.left;    /* destination */
  661.             dy = rect.top;
  662.             wx = rect.right-rect.left; /* width */
  663.             wy = rect.bottom-rect.top;
  664.             sx = rect.left;    /* source */
  665.             sy = rect.top;
  666.             sx += wdev->nHscrollPos; /* scrollbars */
  667.             sy += wdev->nVscrollPos;    
  668.             if (sx+wx > wdev->width)
  669.                 wx = wdev->width - sx;
  670.             if (sy+wy > wdev->height)
  671.                 wy = wdev->height - sy;
  672.             (*wdev->repaint)(wdev,hdc,dx,dy,wx,wy,sx,sy);
  673.             SelectPalette(hdc,oldpalette,NULL);
  674.             EndPaint(hwnd, &ps);
  675.             return 0;
  676.             }
  677. #if WINVER >= 0x030a
  678.         case WM_DROPFILES:
  679.             if (is_win31)
  680.                 SendMessage(hwndtext, message, wParam, lParam);
  681.         case WM_DESTROY:
  682.             /* disable Drag Drop */
  683.             if (is_win31)
  684.                 DragAcceptFiles(hwnd, FALSE);
  685.             break;
  686. #endif
  687.     }
  688.  
  689. not_ours:
  690.     return DefWindowProc((HWND)hwnd, (WORD)message, (WORD)wParam, (DWORD)lParam);
  691. }
  692.  
  693. /* parent overlapped window */
  694. LRESULT CALLBACK _export
  695. WndImgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  696. {
  697.     gx_device_win *wdev;
  698.  
  699.     wdev = (gx_device_win *)GetWindowLong(hwnd, 0);
  700.  
  701.     switch(message) {
  702.         case WM_CREATE:
  703.             wdev =  (gx_device_win *)(((CREATESTRUCT *)lParam)->lpCreateParams);
  704.             wdev->hwndimg = hwnd;
  705.             SetWindowLong(hwnd, 0, (LONG)wdev);
  706. #if WINVER >= 0x030a
  707.             /* Enable Drag Drop */
  708.             if (is_win31)
  709.                 DragAcceptFiles(hwnd, TRUE);
  710. #endif
  711.             break;
  712.         case WM_SYSCOMMAND:
  713.             switch(LOWORD(wParam)) {
  714.                 case M_COPY_CLIP:
  715.                     (*wdev->copy_to_clipboard)(wdev);
  716.                     return 0;
  717.             }
  718.             break;
  719.         case WM_KEYDOWN:
  720.         case WM_KEYUP:
  721.             if (wdev->hwndimgchild) {
  722.                 SendMessage(wdev->hwndimgchild, message, wParam, lParam);
  723.                 return 0;
  724.             }
  725.             break;
  726.         case WM_SIZE:
  727.             if (wParam != SIZE_MINIMIZED  && wdev->hwndimgchild!=(HWND)NULL)
  728.                 SetWindowPos(wdev->hwndimgchild, (HWND)NULL, 0, 0,
  729.                 LOWORD(lParam), HIWORD(lParam), 
  730.                 SWP_NOZORDER | SWP_NOACTIVATE);
  731.             return(0);
  732. #if WINVER >= 0x030a
  733.         case WM_DROPFILES:
  734.             if (is_win31)
  735.                 SendMessage(hwndtext, message, wParam, lParam);
  736.             break;
  737.         case WM_DESTROY:
  738.             if (is_win31)
  739.                 DragAcceptFiles(hwnd, FALSE);
  740. #endif
  741.             break;
  742.     }
  743.     return DefWindowProc(hwnd, message, wParam, lParam);
  744. }
  745.  
  746.